﻿using System;
using System.Collections.Generic;
using System.Data.Entity.SqlServer;
using System.Linq;
using System.Net.Http;
using System.Web;
using Newtonsoft.Json;

namespace WebServiceTMS.Helpers
{
    public class TMSGoal
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string CreatedBy { get; set; }
    }

    public class GoalHelper
    {
        public static HttpResponseMessage AddNewGoalHelper(string goalName, int userId)
        {
            try
            {
                using (var context = new TMSEntities())
                {
                    var user = context.Users.FirstOrDefault(us => us.Id == userId);
                    if (user != null)
                    {
                        context.Goals.Add(new Goals
                        {
                            Name = goalName,
                            CreatedBy = userId
                        });
                        context.SaveChanges();
                        return HttpResponseHelper.CreateHttpResponse("success", "");
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "There is no such user!");
                }
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

        public static HttpResponseMessage RemoveGoalHelper(int goalId, int userId)
        {
            try
            {
                using (var context = new TMSEntities())
                {
                    var user = context.Users.FirstOrDefault(us => us.Id == userId);
                    if (user != null)
                    {
                        if (user.Status == "Admin" || user.Status == "Manager" && user.Id == userId)
                        {
                            var goal = context.Goals.FirstOrDefault(g => g.Id == goalId);
                            if (goal != null)
                            {
                                context.Goals.Remove(goal);
                                context.SaveChanges();
                                return HttpResponseHelper.CreateHttpResponse("success", "");
                            }
                            return HttpResponseHelper.CreateHttpResponse("error", "There is no such goal!");
                        }
                        return HttpResponseHelper.CreateHttpResponse("error", "You have no permisions to remove goals!");
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "There is no such user!");
                }
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

        public static HttpResponseMessage GetAllUserGoalsHelper(int userId)
        {
            try
            {
                using (var context = new TMSEntities())
                {
                    var query = from g in context.Goals
                                join t in context.Teams on g.Id equals t.Goal
                                where t.TeamMember == userId
                                select new TMSGoal
                                {
                                    Name = g.Name,
                                    Id = SqlFunctions.StringConvert((double)g.Id).Trim(),
                                    CreatedBy = SqlFunctions.StringConvert((double)g.CreatedBy).Trim()
                                };
                    return GetJsonGoals(query.ToArray());
                }
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

        public static HttpResponseMessage EditGoalHelper(int goalId, int userId, string newGoalName)
        {
            try
            {
                using (var context = new TMSEntities())
                {
                    var user = context.Users.FirstOrDefault(us => us.Id == userId);
                    if (user != null)
                    {
                        if (user.Status == "Admin" || user.Status == "Manager" && user.Id == userId)
                        {
                            var goal = context.Goals.FirstOrDefault(g => g.Id == goalId);
                            if (goal != null)
                            {
                                if (newGoalName != null)
                                {
                                    goal.Name = newGoalName;
                                    context.SaveChanges();
                                    return HttpResponseHelper.CreateHttpResponse("success", "");
                                }
                                return HttpResponseHelper.CreateHttpResponse("error", "There is error in New Goal Name!");
                            }
                            return HttpResponseHelper.CreateHttpResponse("error", "There is no such goal!");
                        }
                        return HttpResponseHelper.CreateHttpResponse("error", "You have no rights to edit goal!");
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "There is no such user!");
                }
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

        public static HttpResponseMessage GetJsonGoals(TMSGoal[] goalsArray)
        {
            if (goalsArray.Any())
            {
                for (int i = 0; i < goalsArray.Count(); ++i)
                {
                    goalsArray[i].Id = TMSCryptography.Encrypt(goalsArray[i].Id);
                    goalsArray[i].CreatedBy = TMSCryptography.Encrypt(goalsArray[i].CreatedBy);
                }
                var jsonResponse = JsonConvert.SerializeObject(goalsArray);
                return HttpResponseHelper.CreateHttpResponse("success", jsonResponse);
            }
            return HttpResponseHelper.CreateHttpResponse("success", "[]");
        }

    }
}